package com.zq.service;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zq.pojo.MsgInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jms.JmsProperties;
import org.springframework.stereotype.Service;

@Service
public class Publiser {
    private static final Logger logger= LoggerFactory.getLogger(Publiser.class);

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private ObjectMapper objectMapper;

    public void publiser() {
        MsgInfo msgInfo = new MsgInfo();
        msgInfo.setId(1);
        msgInfo.setCode("123");
        msgInfo.setModel("manual");
        Message message = null;
        try {
            message = MessageBuilder.withBody(objectMapper.writeValueAsString(msgInfo).getBytes()).setDeliveryMode(MessageDeliveryMode.PERSISTENT).build();
        } catch (JsonProcessingException e) {
            logger.error("基于manual机制的生产者出现异常");
        }
        rabbitTemplate.setExchange("manualEx");
        rabbitTemplate.setRoutingKey("manualQueue");
        rabbitTemplate.convertAndSend(message);
        logger.info("基于manual机制的生产者发送成功");

    }
}
